<?php
/**
 * Scd Framework
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.njwz.cn/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Eric <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Controller
 * @package    Scd
 * @copyright  Copyright (c) 2007 Blue Web Studio (http://www.njwz.cn)
 * @license    http://framework.njwz.cn/license/     
 * @version    $Id: Scd.php 2007-01-13 02:12:49Z eric $
 */
include_once("Scd/Controller/Exception.php");
include_once("Scd/Uri.php");
/**
 * @name task分配
 */
class Scd_Controller_Route{
	var $_part			= null;
	var $_urlDelimiter	= '/';

	var $_moduleKey     = 'module';
	var $_controllerKey = 'controller';
	var $_actionKey     = 'action';
    var $_values		= array();

    var $_keysSet		= false;
	var $_dispatcher	= true;
	var $_moduleValid	= false;
	
	/**
	 * @name Struct
	 *
	 * @return Scd_Controller_Route
	 */
	function Scd_Controller_Route()
	{
	}
	
	/**
	 * @name factory
	 */
	function factory()
	{
		$uri = new Scd_Uri();
		return $this->math($uri->getPathInfo());
	}

	/**
	 * @name 分配模块
	 */
	function math($path)
	{
		$values = array();
        $params = array();
		$pathStaticCount = 0;
		
		if (count($defaults)) {
			$unique = array_combine(array_keys($defaults), array_fill(0, count($defaults), true));
		} else {
			$unique = array();
		}
		//Strip "/" from the beginning and end of a string
		$path = trim($path, $this->_urlDelimiter);
		if ($path != '') {
			$path = explode($this->_urlDelimiter, $path);

			if ($this->_dispatcher) {
				$values[$this->_moduleKey] = array_shift($path);
				$this->_moduleValid = true;
			}

			if (count($path) && !empty($path[0])) {
				$values[$this->_controllerKey] = array_shift($path);
			}

			if (count($path) && !empty($path[0])) {
				$values[$this->_actionKey] = array_shift($path);
			}
			//获得参数
			if ($numSegs = count($path)) {
				for ($i = 0; $i < $numSegs; $i = $i + 2) {
					$key = urldecode($path[$i]);
					$val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
					$params[$key] = $val;
				}
			}
		}

		$this->_values = $values + $params;

		return $this->_values;
	}
	
	/**
     * Assembles user submitted parameters forming a URL path defined by this route
     *
     * @param array An array of variable and value pairs used as parameters
     * @return string Route path with user submitted parameters
     */
    function assemble($data = array(), $reset = false)
    {
        $params = (!$reset) ? $this->_values : array();

        foreach ($data as $key => $value) {
            if ($value !== null) {
                $params[$key] = $value;
            } elseif (isset($params[$key])) {
                unset($params[$key]);
            }
        }

        $url = '';

        if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
            if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
                $module = $params[$this->_moduleKey];
            }
        }
        unset($params[$this->_moduleKey]);

        $controller = $params[$this->_controllerKey];
        unset($params[$this->_controllerKey]);

        $action = $params[$this->_actionKey];
        unset($params[$this->_actionKey]);

        foreach ($params as $key => $value) {
            $url .= '/' . $key;
            $url .= '/' . $value;
        }

        if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
            $url = '/' . $action . $url;
        }

        if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
            $url = '/' . $controller . $url;
        }

        if (isset($module)) {
            $url = '/' . $module . $url;
        }

        return ltrim($url, $this->_urlDelimiter);
    }
}